﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Wicresoft.UnifyShow.Business;
using Oracle.DataAccess.Client;
using System.Data;

namespace Wicresoft.UnifyShow.BusinessImpl
{
    public class BusinessCategory : Basic, IBusinessCategory
    {
        private const string selectGetAllSiteCategory = "select distinct CategoryName,CategoryAlias from businesscategory";

        public List<BusinessCategoryAndSystemModel> GetBusinessCategoryAndSystem(int siteID, int userID, bool isSortCriticalDesc)
        {
            List<BusinessCategoryAndSystemModel> resultModel = new List<BusinessCategoryAndSystemModel>();
            string sqlStr = string.Empty;
            sqlStr = "select * from businesscategory where siteid = :siteID and siteid in ( select siteid from usersite where userid = :userID )";
            List<OracleParameter> cmdParms = new List<OracleParameter>();
            cmdParms.Add(new OracleParameter("siteID", OracleDbType.Int32, siteID, ParameterDirection.Input));
            cmdParms.Add(new OracleParameter("userID", OracleDbType.Int32, userID, ParameterDirection.Input));
            OracleDataReader odr = oda.ExecuteReader(sqlStr, cmdParms);
            List<BusinessCategoryModel> businessCategoryModel = ModelHelper<BusinessCategoryModel>.GetModel_By_OracleDataReader(odr);
            BusinessSystem bs = new BusinessSystem();
            foreach (BusinessCategoryModel item in businessCategoryModel)
            {
                BusinessCategoryAndSystemModel model = new BusinessCategoryAndSystemModel();
                model.BusinessCategory = item;
                List<BusinessSystemAndAlertModel> bsal = bs.GetBusinessAlertBySiteIdAndCategory((int)item.SiteID, item.CategoryName);
                if (isSortCriticalDesc)
                {
                    bsal.Sort(new SortBSSeverityCriticalDesc());
                }
                else
                {
                    bsal.Sort(new SortBSSeverityCriticalAsc());
                }
                model.BusinessSystemList = bsal;
                model.CriticalCount = bsal.Where(p => p.Severity == (decimal)Severity.Critical).Count();
                model.WarningCount = bsal.Where(p => p.Severity == (decimal)Severity.Warning).Count();
                model.NormalCount = bsal.Where(p => p.Severity == (decimal)Severity.Normal).Count();
                resultModel.Add(model);
            }
            if (isSortCriticalDesc)
            {
                resultModel.Sort(new SortBCSeverityCriticalDesc());
            }
            else
            {
                resultModel.Sort(new SortBCSeverityCriticalAsc());
            }
            //if (p_size.HasValue != false && p_index.HasValue != false)
            //{
            //    return resultModel.Skip((p_index.Value - 1) * p_size.Value).Take(p_size.Value).ToList();
            //}
            return resultModel;
        }


        public List<BusinessCategoryModel> GetAllSiteCategory()
        {
            return ModelHelper<BusinessCategoryModel>.GetModel_By_DirectSql(selectGetAllSiteCategory, null, oda, new List<string>()
                {
                    "CategoryName","CategoryAlias"
                });
        }
    }

    public class SortBSSeverityCriticalDesc : IComparer<BusinessSystemAndAlertModel>
    {
        public int Compare(BusinessSystemAndAlertModel x, BusinessSystemAndAlertModel y)
        {
            if(x.Severity==y.Severity) return x.Alias.CompareTo(y.Alias);
            return y.Severity.CompareTo(x.Severity);
        }
    }
    public class SortBSSeverityCriticalAsc : IComparer<BusinessSystemAndAlertModel>
    {
        public int Compare(BusinessSystemAndAlertModel x, BusinessSystemAndAlertModel y)
        {
            if (x.Severity == y.Severity) return x.Alias.CompareTo(y.Alias);
            return x.Severity.CompareTo(y.Severity);
        }
    }
    public class SortBCSeverityCriticalDesc : IComparer<BusinessCategoryAndSystemModel>
    {
        public int Compare(BusinessCategoryAndSystemModel x, BusinessCategoryAndSystemModel y)
        {
            return y.CriticalCount.CompareTo(x.CriticalCount);
        }
    }
    public class SortBCSeverityCriticalAsc : IComparer<BusinessCategoryAndSystemModel>
    {
        public int Compare(BusinessCategoryAndSystemModel x, BusinessCategoryAndSystemModel y)
        {
            return x.CriticalCount.CompareTo(y.CriticalCount);
        }
    }
}
